package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.SecondaryRequire;

public class SecondaryRequireDAO extends BaseDAO<SecondaryRequire>{

	public long addSecondaryRequire(SecondaryRequire secondary) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_func_Secondary_Require(");
		sb.append("title,description_mc,linkName,publishValidateDate,");
		sb.append("phone,publishDate,remoteIp,userId,username) values (");
		sb.append(":title,:description_mc,:linkName,:publishValidateDate,");
		sb.append(":phone,:publishDate,:remoteIp,:userId,:username)");
		
		update(sb.toString(), secondary);
		
		return getIncreamentId();
	}
	
	public void updateSecondaryRequire(SecondaryRequire secondary) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_func_Secondary_Require set ");
		sb.append(" title=:title,description_mc=:description_mc,");
		sb.append(" linkName=:linkName,phone=:phone,updateDate=:updateDate,");
		sb.append(" publishValidateDate=:publishValidateDate");
		sb.append(" where infoId = :infoId");
		
		update(sb.toString(), secondary);
	}
	
	public List<SecondaryRequire> listAllInfos() throws Exception{
		String sql = "select * from him_func_Secondary_Require";
		return getList(sql, SecondaryRequire.class);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,long userId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from him_func_Secondary_Require");
		StringBuilder countSQL = new StringBuilder("select count(*) from him_func_Secondary_Require  ");
		StringBuilder whereSQL = new StringBuilder(" where (title like ?  or phone like ? or linkName like ?) ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		
		if(userId > 0){
			whereSQL.append(" and userId = ? ");
			paramList.add(userId);
		}
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, SecondaryRequire.class);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,String username) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from him_func_Secondary_Require");
		StringBuilder countSQL = new StringBuilder("select count(*) from him_func_Secondary_Require  ");
		StringBuilder whereSQL = new StringBuilder(" where (title like ?  or phone like ? or linkName like ?) ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		
		if(null != username && !"".equals(username)){
			whereSQL.append(" and username like ? ");
			paramList.add(username);
		}
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, SecondaryRequire.class);
	}
	
	public SecondaryRequire getById(long infoId) throws Exception{
		String sql = "select * from him_func_Secondary_Require where infoId = ?";
		return this.getJavaBean(sql, SecondaryRequire.class, infoId);
	}
	
	public void delSecondaryRequire(long infoId) throws Exception{
		String sql = "delete from him_func_Secondary_Require where infoId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public void setViews(long views,long id) throws Exception{
		String sql = "update him_func_Secondary_Require set views = ? where infoId = ?";
		this.commonUpdate(sql, views,id);
	}
	
	
}
